﻿// order.h : This class represents an order. 
#pragma once

using namespace std;

static char *ItemName[] = { "Kids Drink", "Small Drink", "Med Drink", "Lg Drink", "Jm Drink", "Kids Fries", "Small Fries", "Med Fries", "Large Fries", "Jm Fries", "ChicknBurger", "Beef Burger", "Pie", "Sundae", "Toast" };
float price[] = { 1, 1.25, 1.75, 2.25, 2.75, 1.25, 1.75, 2.25, 2.75, 3, 3.5, 2, 2.5, 1.25, 0.50};

bool QtyIsZeroOrLess (const OrderItem& value) { return (value.quantity<=0); }

class Order
{
        
private:
		OrderManagement* orderTask;
		float total;

public:
		queue<int> orderQueue;
        int orderNumber;  //The order number of this Order. It will also be the file name.
        std::list<OrderItem> orderItems; //http://www.cplusplus.com/reference/stl/list/list

        //Summary: (Constructor) Begins a new order
        //Returns: (nothing)
        Order()
        {
				orderTask = new OrderManagement();
				orderNumber = orderTask->GetOrderNumber();
        }

        //Summary: (Constructor)  Reopens an existing order by using a file name.
        //Inputs : orderNumber = the order number to open;
        //Returns: (nothing)
        Order(int existingOrder)
        {
                //Open the file. The name of the file is the orderNumber.
                orderTask = new OrderManagement(existingOrder);
        }

        //Summary: Adds an item to the Order (overwrite if it already exists)
        //Inputs: itemNumber = The item that we are adding.
        //        qty = The quantity of the item to add. 
        //Returns: (nothing)
        void AddItemToOrder(int itemNumber, int qtyToAdd)
        {
			//check to see if the item is in the list and if so then save its location
			list<OrderItem>::iterator it;
            bool found = false;
            for ( it=orderItems.begin(); it != orderItems.end(); it++ )
                    if(it->itemNumber == itemNumber) 
                    {
                            it->quantity += qtyToAdd;
                            found = true;
                    }   

			//The items was not already in the list - lets add it
			if(!found)
			{	
				OrderItem item;
                item.itemNumber = itemNumber;
                item.quantity = qtyToAdd;
                orderItems.push_back(item);
			}
        }


        //Summary: Removes an item from the Order (overwrite if it already exists)
        //Inputs : intemNumber = The item that we are removing.
        //        qty = The quantity of the item to remove. 
        //Returns: (nothing)
        void RemoveItemToOrder(int itemNumber, int qtyToRemove)
        {
                int count = orderItems.size();
                list<OrderItem>::iterator it;
                //check to see if the item is in the list and if so then save its location
                OrderItem* ptr;
                for ( it=orderItems.begin(); it != orderItems.end(); it++ )
                        if(it->itemNumber == itemNumber) 
                        {
                                it->quantity -= qtyToRemove;
                                if (it->quantity < 0)
                                        it->quantity = 0;
                                
                                ptr = (OrderItem*)&it;
                        }

                //now lets remove any items where the qty is zero
                orderItems.remove_if(QtyIsZeroOrLess);
                
        }


        //Summary: Gets the total number of items in the order. 
        //         (2 fry + 2 sodas would be 4 items)
        //Inputs : (nothing)
        //Returns: total number of items in the order
        int NumberOfItemsInOrder()
        {
                int count = 0;
                list<OrderItem>::iterator it;
                for ( it=orderItems.begin(); it != orderItems.end(); it++ )
                        count += it->quantity;
                return count;
        }


        //Summary: Gets the cart as a list. 
        //Inputs : (nothing)
        //Returns: Returns a many line list of the items in the cart
        string GetCartAsTextList()
        {
				float itemPrice;
                stringstream s;
                list<OrderItem>::iterator it;
                
                itemPrice = 0.0;
                total = 0.0;
                
                for ( it=orderItems.begin(); it != orderItems.end(); it++ )
                {
						itemPrice = price[it->itemNumber] * it->quantity;
						total += itemPrice;
                        s << "Item: " << ItemName[it->itemNumber] << "\tQty: " << it->quantity << "\tPrice: $" << itemPrice << "\r\n";
                }
                
                s << "\r\n" << "Total Price: $" << total;
                return s.str();
        }


        //Summary: Get the total number of different items in the order. 
        //         (2 fry + 2 sodas would be 2 different items)
        //Inputs : (nothing)
        //Returns: total number of items in the order
        int NumberOfDiffItemsInOrder()
        {
                return orderItems.size();;
        }

        //Summary: Dumps an order to a file (overwrite if it already exists)
        //Inputs : (nothing)
        //Returns: (nothing)
        void DumpOrderToFile()
        {
                orderTask->WriteFile(orderItems);
                orderTask->~OrderManagement();

        }


        //Summary: Open a new order
        //Inputs : (nothing)
        //Returns: (nothing)
        void OpenNewOrder()
        {
                

        }
        
		//Summary: Add order number to queue for the pick up window
		//Inputs:  (nothing)
		//Returns: (nothing)
		void AddToQueue()
		{
			orderQueue.push(orderNumber);
		}
		
		//Summary: Get the next order
		//Inputs:  (nothing)
		//Returns: Return the order number that are next in the queue
		int NextInQueue()
		{
			int nextOrder;
			
			nextOrder = orderQueue.front();
			orderQueue.pop();
			
			return nextOrder;
		}
};
